package com.baichang.taoshunda.dao.impl;

import com.baichang.framework.model.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.baichang.framework.dao.impl.BaseDaoImpl;
import com.baichang.taoshunda.dao.ScoreGoodsDao;
import com.baichang.taoshunda.entity.ScoreGoods;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public class ScoreGoodsDaoImpl extends BaseDaoImpl<ScoreGoods> implements ScoreGoodsDao {
    /**
     * 获取所有的积分商品
     *
     * @param nowPage  分页当前页
     * @param pageSize 每页条数 20
     * @return
     */
    @Override
    public List<Map<String, Object>> getScoreGoodsList(Integer nowPage, Integer pageSize) {
        String sql = "SELECT * FROM `t_score_goods` where state=1";
        return listForMapList(sql, nowPage, pageSize);
    }

    /**
     * 根据编号 获取对应的积分商品
     *
     * @param id 商品编号
     * @return
     */
    @Override
    public Map<String, Object> getScoreGoodsById(Integer id) {
        String sql = "SELECT * FROM `t_score_goods` WHERE `id` = ? AND `state` = '1'";
        return queryForMap(sql, id);
    }

    /**
     * 查看对应商品库存
     *
     * @param goodsId 商品编号
     * @return
     */
    @Override
    public int getScoreGoodsStock(int goodsId) {
        String sql = "SELECT stock FROM t_score_goods WHERE state=1 AND id=?";
        return queryForInt(sql, goodsId);
    }

    /**
     * 变动库存
     *
     * @param goodsId 商品编号
     * @param number  库存量
     * @return
     */
    @Override
    public boolean scoreGoodsStock(int goodsId, int number) {
        String sql = "UPDATE `t_score_goods` SET `stock`=? WHERE (`id`=?)";
        return update(sql, number, goodsId) > 0;
    }

    /**
     * 查询商品积分

     * @param name
     * @param nowPage
     * @param pageSize
     * @return
     */
    @Override
    public Page queryGoodsScore(String name, int nowPage, int pageSize) {
        List list = new ArrayList();
        String sql = "SELECT * FROM t_score_goods WHERE state IN (1,2)";
        if (StringUtils.isNotBlank(name)) {
            sql += " AND score_goods_name LIKE ?";
            list.add("%" + name + "%");
        }
        return pageForBeanList(sql, nowPage, pageSize, list.toArray());
    }

    /**
     * 修改积分商品
     *
     * @param map
     * @param id
     * @return
     */
    @Override
    public int update(Map map, Integer id) {
        String sql = "id=?";
        return update(sql, map, id);
    }

    /**
     * 删除
     *
     * @param id
     * @param state
     * @return
     */
    @Override
    public int updateState(Integer id, Integer state) {
        String sql = "UPDATE t_score_goods SET state=? WHERE id=?";
        return update(sql, state, id);
    }
}